//
//  HttpRequest.m
//  NanXiangHome1
//
//  Created by zuqiu on 16/8/16.
//  Copyright © 2016年 zuqiu. All rights reserved.
//

#import "HttpRequest.h"
#import "UIHelper.h"
@implementation HttpRequest

- (NSString *)Base64StrWithMp3Data:(NSData *)data{
    if (!data) {
        NSLog(@"Mp3Data 不能为空");
        return nil;
    }
    //    NSString *str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSString *str = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    return str;
}

+ (void)mapService:(NSDictionary *)dic andToken:(NSString *)token  andView:(UIView *)view httpManagerTodayRequestComplete:(void(^)(NSString *result,NSArray *arr))complete
{
    
    [UIHelper showMessage:@"正在查询..." toView:view];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5.0f;
    
    // 响应的数据格式(JSON
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *paraStr = @"";
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    paraStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //把字典和数组转换成json字符串
    NSDictionary *paraDic = @{@"HTTPJSONKEY":paraStr};
    
    //    NSString *str1 = @"http://demo.finedo.cn/dboss_webapp/finedo/appfile/upload?";
    //    NSString *str2 = [str1 stringByAppendingString:token];
    NSString *str1 = [NSString stringWithFormat:@"%@/fykdweb/finedo/zyygl/queryFormap?token=%@",Base_URL,token];
    
    [manager POST:str1 parameters:paraDic progress:^(NSProgress * _Nonnull uploadProgress) {
        HJLog(@"here");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:NSJSONReadingMutableContainers
                                                               error:nil];
        HJLog(@"==%@",JSON);
        NSString *tag = [NSString stringWithFormat:@"%@",[JSON objectForKey:@"success"]];
        HJLog(@"%@",tag);
        
        
        // 如果成功
        if ([tag isEqualToString:@"1"]) {
            
            HJLog(@"%@",complete);
            if (complete) {
                // 返回成功字符串和token的数据
                NSArray *arr  =JSON[@"object"];
                if (dic.count > 0) {
                      complete(@"success",arr);
                }else{
                     complete(@"success",@[]);
                }
              
            }
            // 不成功，显示登陆结果，返回token为空
        } else {
            [UIHelper dismissLoadingInView:view];
            if (complete) {
                complete([JSON objectForKey:@"resultdesc"],@[]);
            }
        }
        // 可以上传内容，上传完成后再进行下载。
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [UIHelper showErrorWithText:@"与服务器连接失败" inView:view];
        [UIHelper dismissLoadingInView:view];
        
    }];
}



+ (void)sendphoto:(NSDictionary *)dic andToken:(NSString *)token  andView:(UIView *)view httpManagerTodayRequestComplete:(void(^)(NSString *result,NSString *fileName))complete
{
    
    [UIHelper showMessage:@"正在上传..." toView:view];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5.0f;
    
    // 响应的数据格式(JSON
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *paraStr = @"";

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
        paraStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //把字典和数组转换成json字符串
    NSDictionary *paraDic = @{@"HTTPJSONKEY":paraStr};
    
//    NSString *str1 = @"http://demo.finedo.cn/dboss_webapp/finedo/appfile/upload?";
//    NSString *str2 = [str1 stringByAppendingString:token];
    NSString *str1 = [NSString stringWithFormat:@"%@/fykdweb/finedo/photo/upload?token=%@",Base_URL,token];
    
    [manager POST:str1 parameters:paraDic progress:^(NSProgress * _Nonnull uploadProgress) {
        HJLog(@"here");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:NSJSONReadingMutableContainers
                                                               error:nil];
                    HJLog(@"==%@",JSON);
        NSString *tag = [NSString stringWithFormat:@"%@",[JSON objectForKey:@"success"]];
        HJLog(@"%@",tag);
        
        
        // 如果成功
        if ([tag isEqualToString:@"1"]) {
            
            HJLog(@"%@",complete);
            if (complete) {
                // 返回成功字符串和token的数据
                complete(@"success",[[JSON objectForKey:@"object"] objectForKey:@"picname"]);
            }
            // 不成功，显示登陆结果，返回token为空
        } else {
                [UIHelper dismissLoadingInView:view];
            if (complete) {
                complete([JSON objectForKey:@"resultdesc"],@"");
            }
        }
        // 可以上传内容，上传完成后再进行下载。
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        [UIHelper showErrorWithText:@"与服务器连接失败" inView:view];
        [UIHelper dismissLoadingInView:view];

    }];
   }
+ (void)queryJW:(NSDictionary *)dic andToken:(NSString *)token  andView:(UIView *)view httpManagerTodayRequestComplete:(void(^)(NSString *result,NSArray *objectlist))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5.0f;
    
    // 响应的数据格式(JSON
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *paraStr = @"";
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    paraStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //把字典和数组转换成json字符串
    NSDictionary *paraDic = @{@"HTTPJSONKEY":paraStr};
    

    NSString *str1 = [NSString stringWithFormat:@"%@/fykdweb/finedo/map/mapsearch?token=%@",Base_URL,token];
    
    [manager POST:str1 parameters:paraDic progress:^(NSProgress * _Nonnull uploadProgress) {
        HJLog(@"here");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:NSJSONReadingMutableContainers
                                                               error:nil];
        HJLog(@"==%@",JSON);
        NSString *tag = [NSString stringWithFormat:@"%@",[JSON objectForKey:@"success"]];
        HJLog(@"%@",tag);
        
        
        // 如果成功
        if ([tag isEqualToString:@"1"]) {
            
            HJLog(@"%@",complete);
            if (complete) {
                // 返回成功字符串和token的数据
                complete(@"success",[JSON objectForKey:@"object"] );
            }
            // 不成功，显示登陆结果，返回token为空
        } else {
            [UIHelper dismissLoadingInView:view];
            if (complete) {
                complete([JSON objectForKey:@"resultdesc"],@[]);
            }
        }
        // 可以上传内容，上传完成后再进行下载。
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [UIHelper showErrorWithText:@"与服务器连接失败" inView:view];
        
    }];
}


+ (void)editView:(NSDictionary *)dic andToken:(NSString *)token  andView:(UIView *)view httpManagerTodayRequestComplete:(void(^)(NSString *result,NSString *url))complete
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5.0f;
    
    // 响应的数据格式(JSON
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSString *paraStr = @"";
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
     NSString *paraStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //把字典和数组转换成json字符串
    NSDictionary *paraDic = @{@"HTTPJSONKEY":paraStr};
    

    NSString *str1 = [NSString stringWithFormat:@"%@/fykdweb/finedo/zyygl/modifypage?token=%@",Base_URL,token];
    
    [manager POST:str1 parameters:paraDic progress:^(NSProgress * _Nonnull uploadProgress) {
        HJLog(@"here");
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     NSString *urlStr = [[NSString alloc] initWithData:responseObject  encoding:NSUTF8StringEncoding];
        HJLog(@"==%@",urlStr);

        
        
        // 如果成功
        if (urlStr) {
            
            HJLog(@"%@",complete);
            if (complete) {
                // 返回成功字符串和token的数据
                complete(@"success",responseObject);
            }
            // 不成功，显示登陆结果，返回token为空
        } else {
            [UIHelper dismissLoadingInView:view];
            if (complete) {
                complete(@"error",@"");
            }
        }
        // 可以上传内容，上传完成后再进行下载。
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [UIHelper showErrorWithText:@"与服务器连接失败" inView:view];
        [UIHelper dismissLoadingInView:view];
        
    }];
}
@end
